package com.dwjm.mapper;

import com.dwjm.pojo.Student;
import com.dwjm.pojo.StudentVO;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface StudentMapper {
    //根据学生账号查询学生
    @Select("select * from student where username=#{username}")
    Student getByUserName(String username);

    //学生注册
    @Insert("insert into student(studentId,address, name, gender, birthDate, idCardNumber, college, major, className, status, dormId, bedNumber, phone, email, emergencyContact, username, password) " +
            "value(#{studentId},#{address},#{name},#{gender},#{birthDate},#{idCardNumber},#{college},#{major},#{className},#{status},#{dormId},#{bedNumber},#{phone},#{email},#{emergencyContact},#{username},#{password}) ")
    void add(Student student);

    //获取学生列表
    @Select("SELECT " +
            "s.studentId, " +
            "s.name, " +
            "s.gender, " +
            "s.address, " +
            "s.role, " +
            "s.birthDate, " +
            "s.idCardNumber, " +
            "s.college, " +
            "s.major, " +
            "s.className, " +
            "s.status, " +
            "s.dormId, " +
            "s.bedNumber, " +
            "s.phone, " +
            "s.email, " +
            "s.emergencyContact, " +
            "s.username, " +
            "s.password, " +
            "d.building, " +
            "d.floor, " +
            "d.dormNumber " +
            "FROM " +
            "student s " +
            "LEFT JOIN " +
            "dormitory d ON s.dormId = d.dormId " +
            "WHERE " +
            "(#{username} IS NULL OR s.username LIKE CONCAT('%', #{username}, '%')) " +
            "AND (#{address} IS NULL OR s.address LIKE CONCAT('%', #{address}, '%'))")
    List<StudentVO> list(@Param("username") String username, @Param("address") String address);

    //根据学生账号更新学生状态
    @Update("update student set status=#{status} where studentId=#{studentId}")
    void updateStatus(String studentId, String status);

    //更新学生信息
    @Update("UPDATE student SET " +
            "name = #{name}, " +
            "gender = #{gender}, " +
            "address = #{address}, " +
            "birthDate = #{birthDate}, " +
            "idCardNumber = #{idCardNumber}, " +
            "college = #{college}, " +
            "major = #{major}, " +
            "className = #{className}, " +
            "status = #{status}, " +
            "bedNumber = #{bedNumber}, " +
            "phone = #{phone}, " +
            "email = #{email}, " +
            "emergencyContact = #{emergencyContact}, " +
            "username = #{username}, " +
            "password = #{password} " +
            "WHERE studentId = #{studentId}")
    void update(Student student);
}
